<template>
  <div class="page-warpper">
    <el-card>
      <el-row type="flex" justify="space-between" style="margin-bottom:30px">
        <el-col :span="10">
          <!-- 添加按钮 -->
          <el-button type="primary" size="small" @click="isShowDialog=true">添加经纪</el-button>
        </el-col>
        <el-col :span="10">
          <el-row>
            <el-col :span="20">
              <!-- 搜索框 -->
              <el-input v-model="name_contains" placeholder="请输入关键字" />
            </el-col>
            <!-- 搜索按钮 -->
            <el-col :span="4">
              <el-button class="el-icon-search searchbtn" @click="searchBtn" />
            </el-col>
          </el-row>
        </el-col>
      </el-row>
      <hr style="margin-bottom:20px">
      <!-- 表格 -->
      <el-table border :data="list">
        <el-table-column label="图片" prop="image">
          <template v-slot="{row}">
            <el-image :src="row.image" />
          </template>
        </el-table-column>
        <el-table-column label="名称" prop="name" />
        <el-table-column label="邮箱" prop="email" />
        <el-table-column label="电话" prop="phone" />
        <el-table-column label="地址" prop="address" />
        <el-table-column label="介绍" prop="desc" />
        <el-table-column label="操作">
          <template v-slot="{row}">
            <el-button size="mini" type="primary" @click="editAgent(row.id)">编辑</el-button>
            <el-button size="mini" type="danger" @click="deleteAgent(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <el-row type="flex" justify="center">
        <el-pagination :total="total" :page-size="pageConfig._limit" @current-change="currentChange" />
      </el-row>
    </el-card>
    <!-- 弹窗表单 -->
    <el-dialog :visible="isShowDialog" :title="formData.id?'编辑经纪':'新增经纪'" @close="btnCancel">
      <el-form ref="addForm" :rules="rules" :model="formData">
        <el-form-item label="名称" prop="name">
          <el-input v-model="formData.name" style="width:90%" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="formData.email" style="width:90%" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="formData.phone" style="width:90%" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="formData.address" style="width:90%" />
        </el-form-item>
        <el-form-item label="描述" prop="desc">
          <el-input
            v-model="formData.desc"
            type="textarea"
            :rows="5"
            style="width:90%"
          />
        </el-form-item>
        <el-form-item label="封面">
          <UpLoad ref="pageImage" />
          <!-- <el-input v-model="formData.image" placeholder="跳过上传" /> -->
        </el-form-item>
      </el-form>
      <el-row type="flex" justify="end">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getAgents, getAgentsCount, agentDetial, addAgent, editAgent, delAgent } from '@/api/manager'
import UpLoad from './components/Up-Load.vue'
export default {
  components: {
    UpLoad
  },
  data() {
    return {
      name_contains: '',
      list: [],
      total: 0,
      pageConfig: {
        _limit: 2,
        _start: 0
      },
      isShowDialog: false,
      formData: {
        id: '',
        name: '',
        email: '',
        phone: '',
        address: '',
        desc: '',
        image: ''
      },
      rules: {
        name: [{ required: true, message: '该项不能为空', trigger: 'blur' }],
        email: [{ required: true, message: '该项不能为空', trigger: 'blur' }],
        phone: [{ required: true, message: '该项不能为空', trigger: 'blur' }],
        address: [{ required: true, message: '该项不能为空', trigger: 'blur' }],
        desc: [{ required: true, message: '该项不能为空', trigger: 'blur' }]
      }
    }
  },
  async created() {
    this.getList()
  },
  methods: {
    // 获取所有列表
    async getList() {
      const res = await getAgents(this.pageConfig)
      this.list = res.data
      // 获取信息总数
      const res2 = await getAgentsCount()
      // console.log('数量', res)
      this.total = res2.data
      console.log(res)
    },
    // 分页设置
    currentChange(page) {
      const lastPage = page - 1
      const size = lastPage * this.pageConfig._limit
      this.pageConfig._start = size
      // 更新列表
      this.getList()
    },
    // 编辑逻辑
    async editAgent(id) {
      // 获取点击经纪的详情
      const res = await agentDetial(id)
      this.formData = res.data
      this.isShowDialog = true
      this.$nextTick(() => {
        this.$refs.pageImage.fileList = [{ url: this.formData.image }]
      })
      // 显示弹窗
    },
    async searchBtn() {
      // 模糊搜索
      const res = await getAgents({ name_contains: this.name_contains })
      this.list = res.data
      // 获取搜索总数
      const count = await getAgentsCount({ name_contains: this.name_contains })
      this.total = count.data
      // console.log(data)
      console.log(res)
    },
    // 确定按钮
    async btnOk() {
      // 校验
      await this.$refs.addForm.validate()
      if (this.formData.id) {
        // 编辑请求
        this.formData.image = this.$refs.pageImage.fileList[0].url
        await editAgent(this.formData)
        this.$message.success('编辑成功')
      } else {
        // 发送请求
        this.formData.image = this.$refs.pageImage.fileList[0].url
        const res = await addAgent(this.formData)
        console.log(res)
        // 提示用户
        this.$message.success('新增成功')
      }
      // 关闭弹窗
      this.isShowDialog = false
      // 刷新列表
      this.getList()
    },
    // 弹窗的取消按钮
    btnCancel() {
      // 清除表单校验
      this.$refs.addForm.resetFields()
      // 清除表单数据
      this.formData = {
        id: '',
        name: '',
        email: '',
        phone: '',
        address: '',
        desc: ''
      }
      this.$refs.pageImage.fileList = []
      // 关闭表单
      this.isShowDialog = false
    },
    // 删除功能
    async deleteAgent(id) {
      // 二次询问
      await this.$confirm('你确定要删除吗？')
      // 发送请求
      const res = await delAgent(id)
      console.log(res)
      // 提示用户
      this.$message.success('删除成功')
      this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>
.page-warpper{
  margin: 20px;
}

.searchbtn{
  background-color: #f5f7fa;
}

</style>
